package com.example.todolist.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.todolist.common.CheckLogin;
import com.example.todolist.dto.business.PlanCreateDTO;
import com.example.todolist.dto.business.QueryPlanByConditionDTO;
import com.example.todolist.mapper.PlanMapper;
import com.example.todolist.model.Plan;
import com.example.todolist.model.SysUser;
import com.example.todolist.model.TeamFarDO;
import com.example.todolist.response.BaseResponse;
import com.example.todolist.response.SystemResponse;
import com.example.todolist.service.PlanService;
import com.example.todolist.service.SysUserService;
import com.example.todolist.service.impl.WxServiceImpl;
import com.example.todolist.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-02-26
 */

@RestController
@RequestMapping("/plan")
@CrossOrigin
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "事项模块", value = "事项controller")
public class PlanController {

    private final PlanService planService;

    private final SysUserService sysUserService;

    private final PlanMapper planMapper;

    @CheckLogin
    @PostMapping("/planCreate")
    public BaseResponse planCreate(@RequestBody @Validated PlanCreateDTO dto) {
        return planService.planCreate(dto);
    }

    /**
     * 查看当天的
     * @param dto
     * @return
     */
//    @CheckLogin
    @PostMapping("/queryPlanByCondition")
    public BaseResponse queryPlanByCondition(@RequestBody  QueryPlanByConditionDTO dto){
        TeamFarDO teamFarDO = new TeamFarDO();
        teamFarDO.setUid(dto.getOpenid());
        SysUser sysUser = sysUserService.selectByOpenID(teamFarDO);
        dto.setOpenid(sysUser.getUid());
        return planService.queryPlanByCondition(dto);
    }

    /**
     * 用于图标统计
     * @param dto
     * @return
     */
    @PostMapping("/queryPlanGroupByYear")
    public BaseResponse queryPlanGroupByYear(@RequestBody  QueryPlanByConditionDTO dto){
        TeamFarDO teamFarDO = new TeamFarDO();
        teamFarDO.setUid(dto.getOpenid());
        SysUser sysUser = sysUserService.selectByOpenID(teamFarDO);
        dto.setOpenid(sysUser.getUid());
        return planService.queryPlanGroupByYear(dto,sysUser.getWxOpenId());
    }

    /**
     * 查看某个事项的详情
     * @param id
     * @return
     */
    @CheckLogin
    @GetMapping("/queryPlanById")
    public BaseResponse queryPlanById(Long id){
        return planService.queryPlanById(id);
    }

    /**
     * 修改事项的接口。如修改事项的状态
     * @param plan
     * @return
     */
//    @CheckLogin
    @PostMapping("/editPlan")
    public BaseResponse editPlan(@RequestBody Plan plan){
        return planService.editPlan(plan);
    }


    /**
     * 查询个人的所有事项
     * @return
     */
    @CheckLogin
    @GetMapping("/queryPlanByUid")
    @ApiOperation(value = "个人的所有事项")
    public BaseResponse queryPlanByUid(){
        return planService.queryAllByOpenId();
    }

    /**
     * 根据事项类别查询个人的事项
     */
    @PostMapping("/queryPlanByUidAndType")
    @ApiOperation(value = "个人的类型所有事项")
    public BaseResponse queryPlanByUidAndType(@RequestBody  Plan plan){

        SysUser sysUser = sysUserService.selectByOpenId(plan.getUid());

        if (plan.getPlanType().equals("0")) {
            LambdaQueryWrapper<Plan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Plan::getUid,sysUser.getUid());
            List<Plan> planList = planMapper.selectList(lambdaQueryWrapper);
            return SystemResponse.success(planList, planList.size());
        }
        plan.setUid(sysUser.getUid());

        return planService.queryPlanByUidAndType(plan);
    }

    /**
     * 查看所有事项的开始时间
     */
    @GetMapping("/queryAllPlanDate")
    @ApiOperation(value = "查看所有事项的开始时间")
    public BaseResponse queryPlanByUidAndType(String uid){
        SysUser sysUser = sysUserService.selectByOpenId(uid);
        return planService.queryAllPlanDateByUid(sysUser.getUid());
    }

    /**
     * 放弃计划
     */
    @GetMapping("/deletePlanById")
    @ApiOperation(value = "放弃计划")
    public BaseResponse deletePlanById(String id){
        return planService.deletePlanById(id);
    }

}

